package org.jaaksi.customviewdemo.heart

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.Message
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_test_heartrate2.*
import org.jaaksi.customviewdemo.R
import org.jaaksi.customviewdemo.view.WaveView
import kotlin.math.max
import kotlin.math.min

class TestHeartRateActivity2 : AppCompatActivity() {
    private var pointData = mutableListOf<Float>()

    private val maxWaveBand = 3
    private val duration = 1000L

    private val handler = object : Handler(Looper.getMainLooper()) {
        override fun handleMessage(msg: Message) {
            pointData.addAll(generateData(85, pointData.lastOrNull() ?: 0f))
            // 只保留多一个点，用来做动画
            if (pointData.size > (chart.maxWaveBand + 1) * WaveView.MIN_POINT_SIZE) {
                pointData = pointData.subList(
                    pointData.lastIndex - 4 * WaveView.MIN_POINT_SIZE,
                    pointData.lastIndex + 1
                )
            }

            chart.setData(pointData, true)
            sendEmptyMessageDelayed(0, duration)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_test_heartrate2)
        btn_start.setOnClickListener {
            start()
        }

        btn_stop.setOnClickListener {
            stop()
        }

        btn_end.setOnClickListener {
            handler.removeCallbacksAndMessages(null)
            chart.setData(pointData)
        }
        chart.maxWaveBand = maxWaveBand
    }

//    private fun generateInitData(): List<Float> {
//        return (1..chart.maxPointSize).map { 0f }
//    }

    // 一开始全是0，
    private fun generateData(bpm: Int, lastValue: Float): List<Float> {
        // 如果前一个是0，则随机峰谷，如果是峰则谷
        // 75时高度为75%，220时为100%
        val result = when {
            bpm == 75 -> 75f
            bpm > 100 -> min(85f + (bpm - 100f) / (220f - 100f) * 0.15f * 100f, 100f)
            bpm > 75 -> 75f + (bpm - 75f) / (100f - 75f) * 100f * 0.25f
            else -> max(75f - 0.1f * 100f * (75f - bpm) / (75f - 40f), 40f)
        }
        var count = randomInitValue(lastValue)
        return mutableListOf<Float>().apply {
            add(generateSmallValue(count++ % 2 == 0))
            add(generateSmallValue(count++ % 2 == 0))
            add(generateSmallValue(count++ % 2 == 0))
            add(generateSmallValue(count % 2 == 0))
            add(0f)
            add(result)
            add(-result)
            add(0f)
            count = randomInitValue(0f)
            add(generateSmallValue(count++ % 2 == 0))
            add(generateSmallValue(count++ % 2 == 0))
            add(generateSmallValue(count++ % 2 == 0))
            add(generateSmallValue(count++ % 2 == 0))
            add(generateSmallValue(count % 2 == 0))
        }

    }

    private fun randomInitValue(lastValue: Float): Int {
        return when {
            lastValue == 0f -> (0..1).random() // 随机0，1
            lastValue < 0f -> 0
            else -> 1
        }
    }

    private fun generateSmallValue(isPeak: Boolean): Float {
        return (5..15).random().toFloat() * if (isPeak) 1 else -1
    }


//    private fun generateSmallPeakValue(): Float {
//        return Random.nextInt(10, 15).toFloat()
//    }
//
//    private fun generateSmallTroughValue(): Float {
//        return Random.nextInt(-15, -10).toFloat()
//    }

    private fun start() {
        stop()
        pointData.addAll(generateData(80,pointData.lastOrNull() ?: 0f))
        pointData.addAll(generateData(80,pointData.lastOrNull() ?: 0f))
//        dataPoints.addAll(generateData(80,dataPoints.lastOrNull() ?: 0f))
//        dataPoints.addAll(generateInitData())
        chart.setData(pointData)

//        handler.sendEmptyMessage(0)
    }

    private fun stop() {
        handler.removeCallbacksAndMessages(null)
        pointData.clear()
        chart.setData(pointData)
    }

    override fun onDestroy() {
        handler.removeCallbacksAndMessages(null)
        super.onDestroy()
    }


}